package com.admin.kotlintest.base

import android.annotation.TargetApi
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.os.Binder
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import android.view.Window
import android.view.WindowManager
import com.admin.bombtest.LoginActivity
import com.admin.kotlintest.R
import com.admin.kotlintest.config.MyApplication
import com.socks.library.KLog

import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/4/11.
 */

abstract class BaseActivity : AppCompatActivity() {

    val context: Context
        get() = this

    @TargetApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.statusBarColor= resources.getColor(R.color.title,resources.newTheme())
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT// 竖屏
        super.onCreate(savedInstanceState)
        setContentView(bindLayout())
        initData()
        initEvent()
        initConfig()
        MyApplication.addActivityManager(this)
    }



    /**
     * 设置布局文件
     */
    abstract fun bindLayout():Int

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 初始化事件
     */
    abstract fun initEvent()

    private fun initConfig(){
        try{
        iv_title_left.setOnClickListener {
            finish()
        }}catch (e :Exception){
            KLog.d(e.message.toString())
        }
    }

    fun checkPermission(permission: String): Boolean {
        return checkPermission(permission, Binder.getCallingPid(), Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED
    }

    fun isDisabled(){
        toast("你已经被禁止登陆，请联系管理员")
        startActivity<LoginActivity>()
    }
}
